package frame.tool.service;

import frame.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import frame.base.BootstrapTable;
import frame.tool.to.Story;
import frame.tool.qo.StoryQo;
import frame.tool.mapper.StoryMapper;
import tk.mybatis.mapper.common.Mapper;


/**
 * Story 模块服务类
 * <br>
 *
 * @author PeiWentao
 * @since 2020-06-30 11:30
 */
@Service
public class StoryService extends BaseService<Integer, Story, StoryQo> {

    @Autowired
    private StoryMapper storyMapper;

    @Override
    protected Mapper<Story> mainMapper() {
        return storyMapper;
    }

    @Override
    public BootstrapTable<Story> pageList(final StoryQo qo) {
        //分页查询
        Page<Story> page = PageHelper.startPage(qo.page(), qo.size()).doSelectPage(() -> {
            // TODO 分页查询动态条件需要实现
            storyMapper.pageList(qo);
        });
        return new BootstrapTable<Story>(page.getTotal(), page.getResult());
    }

    /**
     * 保存爬虫获取的小说
     *
     * @param story
     */
    public void send(Story story) {
        Story dbStory = storyMapper.getByTitle(story.getTitle());
        if (dbStory == null) {
            story.setDescription("爬虫首次获取");
            super.addSelective(story);
        } else {
            dbStory.setDescription("标题重复，爬虫更新");
            dbStory.setContent(story.getContent());
            super.updateSelective(dbStory);
        }
    }

    @Override
    protected Story newInstanceT(Integer id) {
        return new Story(id);
    }
}
